﻿  
--get the addon namespace
local addon, ns = ...
  
--get the config values
local LabelUI = ns.LabelUI
local cfg = ns.cfg

local toc = LabelUI.toc
local noshow = LabelUI.noshow
local Kill = LabelUI.Kill
local point = LabelUI.point
local textureBackground
local texturehilite
local textureBackground2
local classcolor

local LoadConfig = function()
	textureBackground = cfg.textureBackground
	texturehilite = cfg.texturehilite
	textureBackground2 = cfg.textureBackground2
	classcolor = cfg.classcolor 	
end

local loaded
local addonframe
local indiff
local timer
local timeend
local starttimer
local starttimercount = 0


--[[ Style micro menu ]]

local bar

local MMupdated

local function StyleMicromenu()
    local MicroButtons = {
      "CharacterMicroButton",
      "SpellbookMicroButton",
      "TalentMicroButton",
      "AchievementMicroButton",
      "QuestLogMicroButton",
      "GuildMicroButton",
  --    "PVPMicroButton",
      "LFDMicroButton",
	  "CompanionsMicroButton",
      "MainMenuMicroButton",
      "EJMicroButton",
    }
    if not MMupdated then  
    local prev
    for _, name in pairs(MicroButtons) do
      local bu  = _G[name]
      bu:SetWidth(bar:GetWidth()/10)
      if not bu.bg then 
      bu.bg = CreateFrame("Frame", nil, bu)
      bu.bg:SetPoint("LEFT", bu, 0, 0)
      bu.bg:SetPoint("BOTTOM", bu, 0, 0) 
      bu.bg:SetPoint("RIGHT", bu, 0, 0)
      bu.bg:SetPoint("TOP", Minimap, "BOTTOM", 0, -4.25)
      bu.bg:SetFrameStrata("BACKGROUND")
      bu.bg:SetFrameLevel(bu:GetFrameLevel()-1)
      bu.bg:SetBackdrop(texturehilite)
      bu.bg:SetBackdropColor(0, 0, 0, 0.5)
      bu.bg:SetBackdropBorderColor(0, 0, 0, 0)      
      bu:HookScript("OnEnter", function(self) 
	  		GameTooltip:ClearAllPoints()
			GameTooltip:SetPoint("TOPRIGHT",self, "BOTTOMLEFT") 
			bu.h = true
      end)
      bu:HookScript("OnLeave", function() 
	bu.h = false
      end)
      bu.bg.t = bu.bg:CreateFontString(nil, 'OVERLAY') 
      bu.bg.t:SetFont("Interface\\AddOns\\LabelUI\\media\\calibri.ttf", 25)
      bu.bg.t:SetAllPoints(bu.bg)
      bu.bg.t:SetJustifyH('CENTER')
      bu.bg.t:SetTextColor(classcolor.r, classcolor.g, classcolor.b, 1)
	  if name == "CompanionsMicroButton" then
	  	bu.bg.t:SetText("P")
	  else
      	bu.bg.t:SetText(strsub(name, 1, 1))
	  end
      bu:HookScript("OnUpdate", function(self)
	if bu:GetButtonState() == "PUSHED" then
		bu.bg.t:SetTextColor(184/255, 148/255, 0, 1)
	else
		bu.bg.t:SetTextColor(classcolor.r, classcolor.g, classcolor.b, 1)
	end
	if not bu:IsEnabled() then
		bu.bg:SetBackdropColor(0, 0, 0, 0.5)
	else	
		if bu.h then
			bu.bg:SetBackdropColor(0, 240/255, 1, 0.5)
			if self:GetName() == "MainMenuMicroButton" then
	  			GameTooltip:ClearAllPoints()
				GameTooltip:SetPoint("TOPRIGHT",bu, "BOTTOMLEFT") 	
			end
		else
			bu.bg:SetBackdropColor(0, 0, 0, 0.5)
		end
	end					 
      end)      
      end
      if name == "GuildMicroButton" then
		ic  = _G[name.."Tabard"]
		if ic then
			ic:Hide()
		end
      elseif name == "MainMenuMicroButton" then
	 --MainMenuBarDownload:Hide()
         --MainMenuBarPerformanceBar:Hide()
	 --MainMenuBarDownload.Show = noshow
         --MainMenuBarPerformanceBar.Show = noshow	
		 Kill(MainMenuBarPerformanceBar)
      elseif name == "CharacterMicroButton" then		
		 MicroButtonPortrait:Hide()
      elseif name == "PVPMicroButton" then
		ic  = _G[name.."Texture"]
		if ic then
			ic:Hide()
		end
      end
      bu:SetNormalTexture("")
      bu:SetHighlightTexture("")
      bu:SetPushedTexture("")
      bu:SetDisabledTexture("")
	  hooksecurefunc(bu, "SetNormalTexture",	function(self, tex) if tex ~= "" then self:SetNormalTexture("") end end)
	  hooksecurefunc(bu, "SetHighlightTexture",		function(self, tex) if tex ~= "" then self:SetHighlightTexture("") end end)
	  hooksecurefunc(bu, "SetPushedTexture",	function(self, tex) if tex ~= "" then self:SetPushedTexture("") end end)
	  hooksecurefunc(bu, "SetDisabledTexture",		function(self, tex) if tex ~= "" then self:SetDisabledTexture("") end end)
    end
     if Aurora and not time then
	for i = 1, 4 do
		select(i, TimeManagerFrame:GetRegions()):Hide()
	end
	Aurora[1].SetBD(TimeManagerFrame, 10, -10, -10, 10)
	time = true
     	--Aurora.CreateBD(TimeManagerFrame)
     end
    end
end

--[[from sexy map]]

local buttons = {}
local allChildren = {}
local ignoreButtons = {MinimapPing = true}

local function concatChildren(t, ...)
	for i = 1, select("#", ...) do
		local v = select(i, ...)
		if not ignoreButtons[v] then
			tinsert(t, v)
		end
	end
end

local lastChildCount = 0
local lastChild = nil
local function getChildren()
	local total = Minimap:GetNumChildren() + MinimapBackdrop:GetNumChildren() + MinimapCluster:GetNumChildren()
	if total == lastChildCount then return allChildren end
	lastChildCount = total
	wipe(allChildren)
	concatChildren(allChildren, Minimap:GetChildren())
	concatChildren(allChildren, MinimapBackdrop:GetChildren())
	concatChildren(allChildren, MinimapCluster:GetChildren())
	return allChildren
end

local captureNewChildren
do
	local childCount, lastChild, stockIndex
	-- Buttons to ignore if they show up in iteration. Usually due to manually parenting them to the minimap.
	local ignoreButtons = {
		MiniMapInstanceDifficulty = true,
		MiniMapRecordingButton = true,
		MiniMapTracking = true,
		MiniMapLFGFrame = true,
		GuildInstanceDifficulty = true,
		MiniMapTrackingButton = true,
		MiniMapWorldMapButton = true,
		TimeManagerClockButton = true,
		MinimapZoomIn = true,
		MinimapZoomOut = true,
		MiniMapVoiceChatFrame = true,
		addonframe = true,
		QueueStatusMinimapButton = true,
		MiniMapChallengeMode = true,
	}	
	function captureNewChildren()
		local children = getChildren()
		if #children == childCount and lastChild == children[#children] then return 0 end
		childCount = #children
		lastChild = children[#children]
		
		local count = 0
		for i = (stockIndex or 1), #children do
			local child = children[i]
			local w, h = child.GetWidth and child:GetWidth() or 0, child.GetHeight and child:GetHeight() or 0
			local sizeOk = w > 25 and w < 100 and h > 25 and h < 100
			if sizeOk and stockIndex and not buttons[child:GetName()] and child.SetAlpha and not ignoreButtons[child:GetName()] and not child.sexyMapIgnore and child:GetName() then
				buttons[child:GetName() or ("Button #" .. i)] = {
					chd = child, 
					custom = true,
				}
				count = count + 1
			end
			if child == MiniMapVoiceChatFrame then
				stockIndex = i
			end
		end
		lastChildCount = count
		childCount = count
		return count
	end
end

local function UpdateButtons()
	local count = 0
	local prev
	local up
	for k, v in pairs(buttons) do
		--DEFAULT_CHAT_FRAME:AddMessage(k)
		if v.custom and v.chd then
			v.chd:Show()
			local btn = v.chd
			local havechiled = v.chd:GetChildren()
			if havechiled then
				btn = havechiled
			end
			for i = 1, 3 do
				local frm = select(i, btn:GetRegions())
				if frm then
				local textr = frm:GetTexture()
				if textr == "Interface\\Minimap\\MiniMap-TrackingBorder" or textr == "Interface\\Minimap\\UI-Minimap-Background" then
					frm:Hide()
				else
					frm:SetTexCoord(0.05, 0.95, 0.05, 0.95)				
				end 
				end
			end
			if btn.SetHighlightTexture then
				btn:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\ab_hover")
			end
			v.chd:SetParent(addonframe)
			if not v.onenter then
				v.onenter = btn:GetScript("OnEnter")
			end
			if not v.onleave then
				v.onleave = btn:GetScript("OnLeave")
			end
			btn:SetScript("OnEnter", function()
				timeend = nil
				if v.onenter then
					v.onenter(v.chd)
				end
			end)
			btn:SetScript("OnLeave", function()
				timeend = GetTime() + 2
				if v.onleave then
					v.onleave(v.chd)
				end
			end)
			if not prev then
				v.chd:SetPoint("TOPLEFT", addonframe, "TOPLEFT", -2, -2)
				up = v.chd
			else
				if mod(count, 3) == 0 then
					v.chd:SetPoint("TOPLEFT", up, "BOTTOMLEFT", 0, 0)
					up = v.chd				
				else
					v.chd:SetPoint("TOPLEFT", prev, "TOPRIGHT", 0, 0)
				end
			end
			count = count + 1
			prev = v.chd	
		end
	end
	if count > 3 then
		addonframe:SetWidth(4+31*3)
		addonframe:SetHeight(4+31*(floor((count + 2)/3)))	
	else
		addonframe:SetWidth(4+31*count)
		addonframe:SetHeight(4+31)
	end
end

local function UpdateTimer()
	if timeend then
		if GetTime() > timeend then
			addonframe:Hide()
			timeend = nil
		end
	elseif starttimer then
		if GetTime() > starttimer then
			for k, v in pairs(buttons) do
			end
			local count = captureNewChildren()
			if count > 0 then
				UpdateButtons()
			end
			if starttimercount < 11 then
				starttimercount = starttimercount + 1
				starttimer = 1 + GetTime()
			else
				starttimer = nil
			end
		end
	end
end

--[[ Loading ]]
Wanderlust = CreateFrame("Frame", "Wanderlust", UIParent)

timer = CreateFrame("Frame", nil, Wanderlust)
timer:SetScript("OnUpdate", UpdateTimer)

local function Init()

	LoadConfig()
	
    --[[ Carbonite hint ]]
    local total = 0
    local SetTextureTrick = function(self, elapsed)
        total = total + elapsed
        if(total > 2) then
            if indiff then 
				indiff() 
			else
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE")
			end
            Wanderlust:SetScript("OnUpdate", nil)
        end
    end
    Wanderlust:SetScript("OnUpdate", SetTextureTrick)
    
    --[[ Location and scale ]]
    Minimap:ClearAllPoints()
	Minimap:SetScale(cfg.frames.minimap.Scale)
    Minimap:SetPoint(cfg.frames.minimap.pos.a1, point(cfg.frames.minimap.pos.af), cfg.frames.minimap.pos.a2, cfg.frames.minimap.pos.x / cfg.frames.minimap.Scale, cfg.frames.minimap.pos.y / cfg.frames.minimap.Scale)
    MinimapCluster:EnableMouse(false)

    --[[ Background ]]
    Minimap:SetBackdrop({bgFile = "Interface\\ChatFrame\\ChatFrameBackground", insets = {
        top = -cfg.frames.minimap.BGThickness / cfg.frames.minimap.Scale,
        left = -cfg.frames.minimap.BGThickness / cfg.frames.minimap.Scale,
        bottom = -cfg.frames.minimap.BGThickness / cfg.frames.minimap.Scale,
        right = -cfg.frames.minimap.BGThickness / cfg.frames.minimap.Scale
    }})
     Minimap:SetBackdropColor(cfg.frames.minimap.color.r, cfg.frames.minimap.color.g, cfg.frames.minimap.color.b, cfg.frames.minimap.color.a)

    --[[ Click func ]]
    
    local oldOnClick = Minimap:GetScript("OnMouseUp")
    Minimap:SetScript("OnMouseUp", function(self,click)
	    if(click=="RightButton") then
		    ToggleDropDownMenu(1, nil, MiniMapTrackingDropDown, "cursor", 0, 0)
	    elseif(click=="MiddleButton") then
		    if (not CalendarFrame) then LoadAddOn("Blizzard_Calendar") end Calendar_Toggle() 
	    else 
		    oldOnClick(self)
	    end
    end)

    --[[ Tracking ]]
	MiniMapTracking:ClearAllPoints()
	MiniMapTracking:SetParent(Minimap)
	MiniMapTracking:SetPoint('TOPRIGHT', -20, -2)
	MiniMapTracking:SetAlpha(0)
	MiniMapTrackingBackground:Hide()
	MiniMapTrackingButtonBorder:SetTexture(nil)
	MiniMapTrackingButton:SetHighlightTexture(nil)
	MiniMapTrackingIconOverlay:SetTexture(nil)
	MiniMapTrackingIcon:SetTexCoord(0.065, 0.935, 0.065, 0.935)
	MiniMapTrackingIcon:SetWidth(20)
	MiniMapTrackingIcon:SetHeight(20)
	
    Wanderlust.tracking = CreateFrame("Frame", nil, Minimap)
    Wanderlust.tracking:SetPoint("BOTTOM", 0, 20)
    
    local t = Wanderlust.tracking:CreateFontString(nil, "OVERLAY")
    t:SetFont(cfg.font, cfg.frames.minimap.trackingsize, cfg.frames.minimap.trackingfontflag)
    t:SetPoint("RIGHT")
    t:SetPoint("LEFT")
    
    t:SetTextColor(unpack(cfg.frames.minimap.trackingowncolor))
    
    local function UpdateTrackignText()
        for i = 1, GetNumTrackingTypes() do
            local name, texture, active = GetTrackingInfo(i)
            if(active) then
                t:SetText(name)
                Wanderlust.tracking:SetWidth(t:GetStringWidth()>140 and 140 or t:GetStringWidth())
                Wanderlust.tracking:SetHeight(t:GetStringHeight())
                return
            end
        end
        t:SetText("")
    end

--[[ Instance Difficulty - should work now finally]]
local id = CreateFrame("Frame", nil, UIParent)
id:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 9, -8)
id:RegisterEvent("PLAYER_ENTERING_WORLD")
id:RegisterEvent("PLAYER_DIFFICULTY_CHANGED")
id:RegisterEvent("UPDATE_INSTANCE_INFO")

local idtext = id:CreateFontString(nil, "OVERLAY")
idtext:SetPoint("TOPRIGHT", Minimap, "TOPRIGHT", 0.4, 0.6)
idtext:SetFont(cfg.font, 17) --("Fonts\\FRIZQT__.ttf", 14, "THINOUTLINE")
idtext:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
idtext:SetJustifyH('LEFT')

Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE")
idtext:SetText("") 

function indiff()
	local inInstance, instancetype = IsInInstance()
	local _, type, difficultyIndex, _, _, dynamicDifficulty, isDynamic = GetInstanceInfo()
	if type == "none" then
		Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE")
		idtext:SetText("") 
		return
	end
	if type and type ~= "pvp" and type ~= "arena" then
			if difficultyIndex == 3 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE2")
				idtext:SetText("10") 
			elseif difficultyIndex == 4 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE2")
				idtext:SetText("25") 
			elseif difficultyIndex == 5 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE3")
				idtext:SetText("10H")
			elseif difficultyIndex == 6 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE3")
				idtext:SetText("25H")
			elseif difficultyIndex == 7 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE3")
				idtext:SetText("25L") 
			elseif difficultyIndex == 1 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE1")
				idtext:SetText("5") 
			elseif difficultyIndex == 2 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE2")
				idtext:SetText("5H") 
			elseif difficultyIndex == 8 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE2")
				idtext:SetText("5C") 
			elseif difficultyIndex == 10 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE1")
				idtext:SetText("S") 
			elseif difficultyIndex == 11 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE3")
				idtext:SetText("S H") 
			elseif difficultyIndex == 9 then
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE2")
				idtext:SetText("40") 
			else
				Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE")
				idtext:SetText("")
			end
	end
end

function SETMAPTEXTURE(name, number)
		Minimap:SetMaskTexture("Interface\\AddOns\\LabelUI\\media\\WHITE"..number)
		idtext:SetText(name)
end

id:SetScript("OnEvent", function() indiff() end)

    Minimap:SetScript("OnEnter", function()
        UpdateTrackignText()
		MiniMapTracking:SetAlpha(1)
        Wanderlust.tracking:SetAlpha(1)
    end)
	
		
	MiniMapTrackingButton:SetScript("OnEnter",function()
		UpdateTrackignText()
		MiniMapTracking:SetAlpha(1)
        Wanderlust.tracking:SetAlpha(1)
	end)

    Minimap:SetScript("OnLeave", function()
        MiniMapTracking:SetAlpha(0)
        Wanderlust.tracking:SetAlpha(0)
    end)
	
	MiniMapTrackingButton:SetScript("OnLeave", function()
        MiniMapTracking:SetAlpha(0)
        Wanderlust.tracking:SetAlpha(0)
    end)
	
	MiniMapTrackingButton:SetScript("OnMouseUp", function(self,click)
	    if(click=="RightButton") then
		    ToggleDropDownMenu(1, nil, MiniMapTrackingDropDown, "cursor", 0, 0)
		elseif(click=="MiddleButton") then
			if (not CalendarFrame) then LoadAddOn("Blizzard_Calendar") end Calendar_Toggle() 
		end
	end)


    Wanderlust.tracking.text = t
	
--[[ Clock ]]
if not IsAddOnLoaded("Blizzard_TimeManager") then
	LoadAddOn("Blizzard_TimeManager")
end
local clockFrame, clockTime = TimeManagerClockButton:GetRegions()
clockFrame:Hide()
clockTime:SetFont(cfg.font, 14) --"Fonts\\FRIZQT__.ttf" "THINOUTLINE"
clockTime:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
clockTime:SetJustifyH('CENTER')
clockTime:SetShadowColor(0,0,0,0)
TimeManagerClockButton:ClearAllPoints()
TimeManagerClockButton:SetPoint("BOTTOM", Minimap, "BOTTOM", -2.7, -10)
TimeManagerClockButton:SetScript('OnShow', nil)
TimeManagerClockButton:Show()
TimeManagerClockButton:SetHitRectInsets(10, 8, 0, 10)
TimeManagerClockButton:SetScript('OnClick', function(self, button)
	 if(button=="RightButton") then
		    ToggleDropDownMenu(1, nil, MiniMapTrackingDropDown, "cursor", 0, 0)
	 elseif(button == 'MiddleButton') then
		ToggleCalendar()
	else
		if(self.alarmFiring) then
			PlaySound('igMainMenuQuit')
			TimeManager_TurnOffAlarm()
		else
			ToggleTimeManager()
		end
	end
end)

    
--[[ Calendar ]]

  local OnEnter_date = function(self)
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOM")
        GameTooltip:ClearLines()
        if ( CalendarGetNumPendingInvites() ~= 0 ) then
            GameTooltip:AddLine(GAMETIME_TOOLTIP_CALENDAR_INVITES)
            if ( CalendarFrame and not CalendarFrame:IsShown() ) then
                GameTooltip:AddLine(" ")
                GameTooltip:AddLine(GAMETIME_TOOLTIP_TOGGLE_CALENDAR)
            end
        else
            GameTooltip:AddLine(GAMETIME_TOOLTIP_TOGGLE_CALENDAR)
        end
        GameTooltip:Show()
  end

  local OnLeave_date = function()
	GameTooltip:Hide()
  end

  local function OnUpdate_date(self)
     self.text:SetText(date("%d.%m"))
     	if CalendarGetNumPendingInvites() ~= 0 then
		self.text:SetTextColor(0, 1, 0)
	else
		self.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
	end
  end

  local function Onclicl_calendar(self)
    if ( CalendarGetNumPendingInvites() ~= 0 ) then
        Calendar_LoadUI()
        if ( Calendar_Show ) then
            Calendar_Show()
        end
        --GameTimeCalendarInvitesTexture:Hide()
        --GameTimeCalendarInvitesGlow:Hide()
        GameTimeFrame.pendingCalendarInvites = 0
        GameTimeFrame.flashInvite = false
    else
        ToggleCalendar()
    end
  end

  
   local calendar = CreateFrame("Button", nil, Minimap)
   calendar:SetPoint("TOP", Minimap, "TOP", -0.5, 8.5)
   calendar:SetSize(75, 30)
   calendar:SetScript("OnClick", Onclicl_calendar) 
   calendar.BG = calendar:CreateTexture(nil, "BACKGROUND")
   calendar.BG:SetAllPoints(calendar)
   calendar.BG:SetTexture(0, 0, 0, 0)
   calendar.text = calendar:CreateFontString(nil, 'OVERLAY') 
   calendar.text:SetFont(cfg.font, 14) --"Fonts\\FRIZQT__.ttf" "THINOUTLINE"
   calendar.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
   calendar.text:SetJustifyH('CENTER')
   calendar.text:SetPoint('CENTER',calendar,'CENTER', 0, 0)
   calendar.text:SetText(date("%d.%m"))
   calendar:SetScript("OnUpdate", OnUpdate_date)
   calendar:SetScript("OnEnter", OnEnter_date)
   calendar:SetScript("OnLeave", OnLeave_date)	


--[[ Addon frame ]]

local OnEnter_anfbutton = function(self)
	GameTooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT")
    GameTooltip:AddLine("Addons")
	GameTooltip:Show()
end

local OnLeave_anfbutton = function(self)
	GameTooltip:Hide()
end

addonframe = CreateFrame("Frame", nil, Minimap)
addonframe:SetWidth(80)
addonframe:SetHeight(60)
addonframe:SetPoint("TOPRIGHT", Minimap, "TOPLEFT", -(cfg.frames.minimap.BGThickness/cfg.frames.minimap.Scale), 0)
addonframe.BG = addonframe:CreateTexture(nil, "BACKGROUND")
addonframe.BG:SetAllPoints(addonframe)
addonframe.BG:SetTexture(0, 0, 0, 0.5)
addonframe:Hide()
local framechr = _G["CharacterMicroButton"]
addonframe:SetFrameLevel(framechr:GetFrameLevel() + 1)
addonframe:SetScript("OnLeave", function()
	timeend = GetTime() + 2
end)
addonframe:SetScript("OnEnter", function()
	timeend = nil
end)

local anfbutton = CreateFrame("Button", nil, Minimap)
anfbutton:SetWidth(16)
anfbutton:SetHeight(16)
anfbutton:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 0, 0)
anfbutton.text = anfbutton:CreateFontString(nil, 'OVERLAY') 
anfbutton.text:SetFont(cfg.font, 18) --"Fonts\\FRIZQT__.ttf" , "THINOUTLINE"
anfbutton.text:SetTextColor(classcolor.r, classcolor.g, classcolor.b)
anfbutton.text:SetJustifyH('CENTER')
anfbutton.text:SetPoint('CENTER',anfbutton,'CENTER', -1.5, -2)
anfbutton.text:SetText("*")
anfbutton:SetScript("OnClick", function()
	timeend = GetTime() + 2
	addonframe:Show()
end)
anfbutton:SetScript("OnEnter", OnEnter_anfbutton)
anfbutton:SetScript("OnLeave", OnLeave_anfbutton)



local count = captureNewChildren()
if count > 0 then
	UpdateButtons()
end
starttimer = GetTime() + 1

loaded = true

--[[ Zone Text ]]

MinimapZoneText:ClearAllPoints()
MinimapZoneText:SetAllPoints()
MinimapZoneTextButton:SetHeight(26)
MinimapZoneTextButton:SetBackdrop({bgFile = "Interface\\ChatFrame\\ChatFrameBackground", insets = {
        top = cfg.frames.minimap.BGThickness,
        left = -cfg.frames.minimap.BGThickness,
        bottom = cfg.frames.minimap.BGThickness,
        right = -cfg.frames.minimap.BGThickness,
    }})
MinimapZoneTextButton:SetFrameStrata("MEDIUM")
MinimapZoneTextButton:ClearAllPoints()
MinimapZoneTextButton:SetPoint("BOTTOM", Minimap, "TOP", 0, 0)
MinimapZoneTextButton:SetWidth(Minimap:GetWidth()*(cfg.frames.minimap.Scale))
MinimapZoneText:SetFont(cfg.font, 14)
MinimapZoneText:SetShadowColor(0,0,0,0)
MinimapZoneTextButton:SetBackdropColor(0, 0, 0, 0.5)
MinimapZoneTextButton:SetBackdropBorderColor(0, 0, 0, 0)

--[[ Micro menu ]]

    bar = CreateFrame("Frame","LUI_MicroMenu",UIParent, "SecureHandlerStateTemplate")
    bar:SetWidth((cfg.frames.minimap.BGThickness/cfg.frames.minimap.Scale/0.58)*2 + Minimap:GetWidth()/0.58)
    bar:SetHeight(40)
    bar:SetPoint("TOPLEFT","Minimap","BOTTOMLEFT", -(cfg.frames.minimap.BGThickness/cfg.frames.minimap.Scale/0.58), -1)
    bar:SetHitRectInsets(0, 0, 0, 0)
    bar:SetParent(Minimap)
    bar:SetFrameLevel(Minimap:GetFrameLevel())   
    bar:SetScale(0.58)
	bar.Hide = function() end 
  
   -- StoreMicroButton
	--StoreMicroButton:ClearAllPoints()
	--HelpMicroButton:ClearAllPoints()
	StoreMicroButton:SetPoint("BOTTOMLEFT",EJMicroButton,"BOTTOMRIGHT", 0, 0)
	StoreMicroButton:ClearAllPoints()
	StoreMicroButton.SetPoint = function() end
    local MicroButtons = {
      "CharacterMicroButton",
      "SpellbookMicroButton",
      "TalentMicroButton",
      "AchievementMicroButton",
      "QuestLogMicroButton",
      "GuildMicroButton",
   --   "PVPMicroButton",
      "LFDMicroButton",
	  "CompanionsMicroButton",
      "MainMenuMicroButton",
      "EJMicroButton",
    }  

    local function movebuttons() 
      local prev
      for _, name in pairs(MicroButtons) do
	    local f = _G[name]
        f:SetParent(bar)
        if prev then
		f:ClearAllPoints()
		f:SetPoint("LEFT",prev,"RIGHT", 0, 0)
		--f:SetPoint("BOTTOMLEFT",prev,"BOTTOMRIGHT", 0, 0)
		--f:SetPoint("BOTTOM", 0, 0)
		--f:SetPoint("TOP", 0, 0)
		--f.ClearAllPoints = function() end
		--hooksecurefunc(f, "SetPoint",		function(self, _, frm) if frm ~= prev then self:ClearAllPoints(); self:SetPoint("LEFT",prev,"RIGHT", 0, 0) end end)
		--f.SetPoint = function() end
        else
		f:ClearAllPoints()
		f:SetPoint("BOTTOMLEFT", 0, 0)
		--f.ClearAllPoints = function() end
		--hooksecurefunc(f, "SetPoint",		function(self, pnt, a) if pnt ~= "BOTTOMLEFT" and a ~= 0 then self:ClearAllPoints(); self:SetPoint("BOTTOMLEFT", 0, 0) end end)
		--f.SetPoint = function() end
        end
		--f.SetPoint = function() end
		--f.Hide = function() end
		--f.SetParent = function() end
		--f.ClearAllPoints = function() end
	    prev = f
      end
      --CharacterMicroButton:ClearAllPoints()
      --CharacterMicroButton:SetPoint("BOTTOMLEFT", 0, 0)
    end
    
    movebuttons()
    
    bar:SetScript("OnEvent", function(self,event) 
      if event == "PLAYER_TALENT_UPDATE" or event == "ACTIVE_TALENT_GROUP_CHANGED" then
        if not InCombatLockdown() then
          movebuttons()
        end        
      end
    end)
    
    bar:RegisterEvent("PLAYER_TALENT_UPDATE")
    bar:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")

    hooksecurefunc("UpdateMicroButtons",		StyleMicromenu)
	
	local LUIWatchFrame = CreateFrame("Frame", "TukuiWatchFrame", UIParent) --watchframe position, come from Tukui
	LUIWatchFrame:SetHeight(700)
	LUIWatchFrame:ClearAllPoints()
	LUIWatchFrame:SetPoint("TOPRIGHT", "LUI_MicroMenu", "BOTTOM", 0, -20)
	--LUIWatchFrame:SetWidth(250)
	--local wideFrame = GetCVar("watchFrameWidth")
	--if wideFrame == 1 then
		LUIWatchFrame:SetWidth(300)
	--else
	--	LUIWatchFrame:SetWidth(250)
	--end
	--LUIWatchFrame:RegisterEvent("CVAR_UPDATE")
--	LUIWatchFrame:SetScript("OnEvent", function(_,_,cvar,value)
--		if cvar == "WATCH_FRAME_WIDTH_TEXT" then
--					LUIWatchFrame:SetWidth(350)
--				else
--					LUIWatchFrame:SetWidth(250)
--				end
--			end
--		end
--	end)
	
	
	Wanderlust:SetScript("OnEvent", function(self, event, arg1)  
		LUIWatchFrame:SetHeight(bar:GetBottom() - 300)
		ObjectiveTrackerFrame:ClearAllPoints()
		ObjectiveTrackerFrame:SetParent(LUIWatchFrame)
		ObjectiveTrackerFrame:SetFrameStrata("LOW")
		ObjectiveTrackerFrame:SetFrameLevel(3)
		ObjectiveTrackerFrame:SetClampedToScreen(false)
		ObjectiveTrackerFrame.ClearAllPoints = function() end
		ObjectiveTrackerFrame:SetPoint("TOPLEFT", 32,-2.5)
		ObjectiveTrackerFrame:SetPoint("BOTTOMRIGHT", 4,0)
		ObjectiveTrackerFrame.SetPoint = function() end
		ObjectiveTrackerFrame:UnregisterEvent("PLAYER_ENTERING_WORLD")
	end)
	
	Wanderlust:RegisterEvent("PLAYER_ENTERING_WORLD")
	
	--[[ Durability Frame ]]--
	DurabilityFrame.trueSetPoint = DurabilityFrame.SetPoint
	DurabilityFrame.SetPoint = function()
		DurabilityFrame:trueSetPoint("TOPRIGHT", "LUI_MicroMenu", "BOTTOMRIGHT", -15 , -15)
	end
	
	
	--[[ Vehicle Seat Indicator ]]--
	VehicleSeatIndicator.trueSetPoint = VehicleSeatIndicator.SetPoint
	VehicleSeatIndicator.SetPoint = function()
		VehicleSeatIndicator:trueSetPoint("TOPRIGHT", "LUI_MicroMenu", "BOTTOMRIGHT", -15 , -15)
	end
	

end   --[[ END INIT FUNCTION ]]--
LabelUI:AddInit(Init, "MiniMap")

function GetMinimapShape() return "SQUARE" end

--[[ Hiding ugly things	]]
local dummy = function() end
local frames = {
    "MiniMapVoiceChatFrame",
    "MiniMapWorldMapButton",
    "MiniMapMailBorder",
    "MinimapBorderTop",
    "MiniMapInstanceDifficulty",
    "MinimapNorthTag",
    "MinimapZoomOut",
    "MinimapZoomIn",
    "MinimapBorder",
    "GameTimeFrame",
	"GuildInstanceDifficulty",
}
GameTimeFrame:SetAlpha(0)
GameTimeFrame:EnableMouse(false)

for i in pairs(frames) do
    _G[frames[i]]:Hide()
    _G[frames[i]].Show = dummy
end

--[[ Mousewheel zoom ]]
Minimap:EnableMouseWheel(true)
Minimap:SetScript("OnMouseWheel", function(_, zoom)
    if zoom > 0 then
        Minimap_ZoomIn()
    else
        Minimap_ZoomOut()
    end
end)

--[[ BG icon ]]
if (toc < 50000) then
MiniMapBattlefieldFrame:ClearAllPoints()
MiniMapBattlefieldFrame:SetParent(Minimap)
MiniMapBattlefieldFrame:SetPoint('TOPRIGHT', 2, -2)
MiniMapBattlefieldBorder:SetTexture(nil)
BattlegroundShine:Hide()

--[[ Random Group icon ]]
		--QueueStatusMinimapButton
		--MiniMapChallengeMode 
		--QueueStatusMinimapButton
		--/run local kids = { QueueStatusMinimapButton:GetChildren() };for _, bg in ipairs(kids) do print("1",bg:GetName()); end
MiniMapLFGFrame:ClearAllPoints()
MiniMapLFGFrame:SetParent(Minimap)
MiniMapLFGFrame:SetPoint('TOPRIGHT', 2, -2)
MiniMapLFGFrame:SetHighlightTexture(nil)
end
QueueStatusMinimapButton:ClearAllPoints()
QueueStatusMinimapButton:SetParent(Minimap)
QueueStatusMinimapButton:SetPoint('TOPRIGHT', 2, -2)
QueueStatusMinimapButton:SetHighlightTexture(nil)
QueueStatusMinimapButtonBorder:SetTexture(nil)
--[[ Mail icon ]]
MiniMapMailFrame:ClearAllPoints()
MiniMapMailFrame:SetPoint("BOTTOMRIGHT", Minimap, "BOTTOMRIGHT", 6, -8)
MiniMapMailIcon:SetTexture("Interface\\AddOns\\LabelUI\\media\\mail")
